﻿var remote = {
	getSets: function(id, callback) { flickr.getSetList(id, callback); },
	getSet: function(id, callback) { flickr.getSet(id, callback); },
	getPhoto: function(id, callback) { flickr.getPhoto(id, callback); }
};

ti.menu.newItem('Flickr', ['editable']);
var _validateMenu = ti.menu.addItem('Validate Links', defaultHandler);
var _insertMenu = ti.menu.addItem('Insert Images', defaultHandler);
var _optionMenu = ti.menu.addItem('Options', defaultHandler);

chrome.extension.onRequest.addListener(function(request, sender, callback) {
	remote[request.action](request.id, callback);
});

function defaultHandler(info, tab) {
	var method = null;
	switch (info.menuItemId) {
		case _insertMenu: method = "insert"; break;
		case _validateMenu: method = "validate"; break;
		case _optionMenu: method = "options"; break;
	}
	chrome.tabs.sendRequest(
		tab.id,
		{ "method": method, "user": ti.data.get('name') },
		function(r) {
			if (info.menuItemId == _optionMenu) {
				chrome.tabs.create({url: 'chrome-extension://' + r + '/options.html'});
			}
	});
}

